from flask import Flask, render_template, request, jsonify
from flask_cors import CORS

app = Flask(__name__, template_folder="templates", static_folder="static")

CORS(app, resources=r'/*')


def error(text):
    return jsonify({'error': 1, 'data': text}), 400


def resjosn(data):
    return jsonify({'error': 0, 'data': data}), 200


@app.route('/')
def index():
    return render_template("index.html")


@app.route('/api/test', methods=['GET'])
def apitest():
    return resjosn("Hello World")

# @app.route('/api/functions', methods=['GET'])
# def apitest_post():
#
#     return resjosn("Hello World")


@app.route('/api/pdf/hbuplaod', methods=['POST'])
def apitest():
    # pdf合并
    if 'file' not in request.files:
        return error("未找到文件！请重新上传")

    file = request.files['file']
    if file.filename == '':
        return error("未找到文件！请重新上传")

    if file:
        # filename = secure_filename(file.filename)
        # file.save(os.path.join('/path/to/upload', filename))
        pass
        return resjosn("")


@app.route('/api/pdf/sbuplaod', methods=['POST'])
def apitest():
    # pdf识别
    if 'file' not in request.files:
        return error("未找到文件！请重新上传")

    file = request.files['file']
    if file.filename == '':
        return error("未找到文件！请重新上传")

    if file:
        # filename = secure_filename(file.filename)
        # file.save(os.path.join('/path/to/upload', filename))
        pass
        return resjosn("")


@app.route('/api/pdf/tmuplaod', methods=['POST'])
def apitest():
    # pdf脱敏
    if 'file' not in request.files:
        return error("未找到文件！请重新上传")

    file = request.files['file']
    if file.filename == '':
        return error("未找到文件！请重新上传")

    if file:
        # filename = secure_filename(file.filename)
        # file.save(os.path.join('/path/to/upload', filename))
        pass
        return resjosn("")
